package org.xhy.hrm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xhy.hrm.dto.AlipayNotifyDto;
import org.xhy.hrm.service.IPayBillService;
import org.xhy.hrm.service.IPayService;
import org.xhy.hrm.util.AjaxResult;

@RestController
@RequestMapping("/alipay")
public class PayController {

    @Autowired
    private IPayService payService;

    @Autowired
    private IPayBillService payBillService;

    //发起支付申请
    @RequestMapping("/apply/{orderNo}")
    public AjaxResult apply(@PathVariable("orderNo")String orderNo){
        return AjaxResult.me().setResultObj(payService.apply(orderNo));
    }

    //同步回调
    @RequestMapping("/return")
    public AjaxResult payReturn(String out_trade_no){
        //根据支付状态来返回结果，写sql来查询
        //PayBill payBill = payBillService.selectByPayNo(out_trade_no);
        return AjaxResult.me().setResultObj("支付成功！");
    }

    //异步回调
    @RequestMapping("/notify")
    public String payNotify(AlipayNotifyDto alipayNotifyDto){
        //不能交给全局捕获异常，那是返回ajax
        try {
            return payService.payNotify(alipayNotifyDto);
        } catch (Exception e){
            e.printStackTrace();
            return "fail";
        }
        //支付宝异步回调，只要返回success这个词，支付宝就认为支付成功，只要不是就会重试，最大努力通知
        //return "success";
    }


}
